%% Aim
% Generate pupil curves by choices

%% Setup
tic;
clc;
clear;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;
nblock=2;
ntrial=100;
nprice=10;
nlottery=10;
nchoice=2;
nending=2;

role_label={'Buyer','Seller','Contrast'};
role_color_dark={[235/255 112/255 99/255],[106/255 142/255 208/255]}; 
role_color_median={[238/255 137/255 126/255],[144/255 171/255 220/255],[0.75 0.75 0.75]}; 
role_color_light={[243/255 171/255 163/255],[165/255 187/255 227/255]};

attribute_label={'Price','Lottery'};
attribute_color={[91/255,155/255,213/255],[112/255,173/255,71/255],[166/255,166/255,166/255]}; % Price vs. Lottery

bias_label={'Val','Res','Val vs. Res'};
bias_color_median={[195/255 205/255 147/255],[192/255 173/255 203/255]}; 

choice_label={'Accept','Reject'};
choice_color_dark={[235/255 112/255 99/255],[106/255 142/255 208/255]
                   [106/255 142/255 208/255],[235/255 112/255 99/255]}; 
choice_color_median={[238/255 137/255 126/255],[144/255 171/255 220/255]
                     [144/255 171/255 220/255],[238/255 137/255 126/255]}; 
choice_color_light={[243/255 171/255 163/255],[165/255 187/255 227/255]
                    [165/255 187/255 227/255],[243/255 171/255 163/255]};

%% Load data
sub_data=readtable([root_path,'\data\subject\subject.csv']);
sub_beh_data=readtable([root_path,'\data\subject\subject_behavior.csv']);
sub_ddm_data=readtable([root_path,'\data\subject\subject_ddm.csv']);
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
pupil_trial_data=readtable([root_path,'\data\pupil\pupil_trial.csv']);
eye_trial_data=readtable([root_path,'\data\eye\eye_trial.csv']);
load([root_path,'\data\pupil\pupil_trial_curve.mat']);
nsub=height(sub_data);

%% Concatenated data
sub_data = [sub_data, sub_beh_data(:,4:end), sub_ddm_data(:,4:end)];
trial_data = [beh_data, eye_trial_data(:,9:end), pupil_trial_data(:,9:end)];

%% whole time window -500 ms before to 4000 after option onset: subject

x=-0.5:0.002:4;
rt=[sub_data.RTBuyYes(:),sub_data.RTBuyNo(:),sub_data.RTSellYes(:),sub_data.RTSellNo(:)];

% Each subject
for isub=1:nsub 
    figure('Renderer', 'painters', 'Position', [10 10 900 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            tdata=trial_data(trial_data.Subject==isub & trial_data.Role==irole & trial_data.Choice==2-ichoice & trial_data.ValidPupil==1,:);
            whole4000{isub,irole,ichoice}=[];
            for t=1:height(tdata)
                whole4000{isub,irole,ichoice}=[whole4000{isub,irole,ichoice};...
                                               pupil_trial_curve.Subject(isub).Role(irole).WholeOption4000{(tdata.Price(t)),(tdata.Lottery(t))/2}.Pupilz(:)'];
            end            
            whole4000_mean{isub,irole,ichoice}=mean(whole4000{isub,irole,ichoice});
            whole4000_se{isub,irole,ichoice}=std(whole4000{isub,irole,ichoice})/sqrt(length(whole4000{isub,irole,ichoice}(:,1))-1);        
        
            %plot
            [l,p] = boundedline(x, whole4000_mean{isub,irole,ichoice}, whole4000_se{isub,irole,ichoice},'k','alpha');
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
            line([rt(isub,(irole-1)*2+ichoice) rt(isub,(irole-1)*2+ichoice)],ylim,'Color',choice_color_dark{irole,ichoice},'LineStyle','--');        
        end
        
        set(ax,'xlim', [-0.5 4]);
        xticks(-0.5:0.5:4);
        set(ax,'TickDir','out');
        xlabel('Time relative to option onset (seconds)');
        ylabel('Pupil size (adjusted)');
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on        
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\whole4000\curve\s',num2str(isub),'_pupil_whole4000.tif'], '-r200');
    hold off;   
    close;
end

%% whole time window -500 ms before to 4000 after option onset: group

x=-0.5:0.002:4;

sdata=sub_data;
rt=[mean(sdata.RTBuyYes),mean(sdata.RTBuyNo),mean(sdata.RTSellYes),mean(sdata.RTSellNo)];

    figure('Renderer', 'painters', 'Position', [10 10 850 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            whole4000_group{irole,ichoice}=[];
            for isub=1:nsub
                whole4000_group{irole,ichoice}=[whole4000_group{irole,ichoice};whole4000{isub,irole,ichoice}];
            end            
            whole4000_group_mean{irole,ichoice}=mean(whole4000_group{irole,ichoice});
            whole4000_group_se{irole,ichoice}=std(whole4000_group{irole,ichoice})/sqrt(length(whole4000_group{irole,ichoice}(:,1))-1);
            %plot
            [l,p] = boundedline(x, whole4000_group_mean{irole,ichoice}, whole4000_group_se{irole,ichoice},'k','alpha');
            l.LineWidth=1.5;
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
            line([rt(1,(irole-1)*2+ichoice) rt(1,(irole-1)*2+ichoice)],[-1.2 0.2],'Color',choice_color_dark{irole,ichoice},'LineStyle','--');        
        end
        
        set(ax,'xlim', [-0.5 3]);
        xticks(-0.5:0.5:3);
        set(ax,'TickDir','out');
        xlabel('Time relative to option onset (seconds)');
        ylim([-1.2 0.2]);
        ylabel('Pupil size (adjusted)');
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\whole4000\curve\group_pupil_whole4000_across_trials.tif'], '-r200');
    hold off;   
    close;

    %% whole time window -500 ms before to 4000 after option onset: group_across_subjects

x=-0.5:0.002:4;

sdata=sub_data;
rt=[mean(sdata.RTBuyYes),mean(sdata.RTBuyNo),mean(sdata.RTSellYes),mean(sdata.RTSellNo)];

    figure('Renderer', 'painters', 'Position', [10 10 900 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            whole4000_group{irole,ichoice}=[];
            for isub=1:nsub
                whole4000_group{irole,ichoice}=[whole4000_group{irole,ichoice};whole4000_mean{isub,irole,ichoice}];
            end            
            whole4000_group_mean{irole,ichoice}=mean(whole4000_group{irole,ichoice});
            whole4000_group_se{irole,ichoice}=std(whole4000_group{irole,ichoice})/sqrt(length(whole4000_group{irole,ichoice}(:,1))-1);
            %plot
            [l,p] = boundedline(x, whole4000_group_mean{irole,ichoice}, whole4000_group_se{irole,ichoice},'k','alpha');
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
            line([rt(1,(irole-1)*2+ichoice) rt(1,(irole-1)*2+ichoice)],ylim,'Color',choice_color_dark{irole,ichoice},'LineStyle','--');        
        end
        
        set(ax,'xlim', [-0.5 3]);
        xticks(-0.5:0.5:3);
        set(ax,'TickDir','out');
        xlabel('Time relative to option onset (seconds)');
        ylabel('Pupil size (adjusted)');
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\whole4000\curve\group_pupil_whole4000_across_subjects.tif'], '-r200');
    hold off;   
    close;

%% Decision pupil -500 ms to 1500 ms locked to decision: subject

x=-0.75:0.002:1.5;

% Each subject
for isub=1:nsub 
    figure('Renderer', 'painters', 'Position', [10 10 900 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            tdata=trial_data(trial_data.Subject==isub & trial_data.Role==irole & trial_data.Choice==2-ichoice & trial_data.ValidPupil==1,:);
            dec_option{isub,irole,ichoice}=[];
            for t=1:height(tdata)
                dec_option{isub,irole,ichoice}=[dec_option{isub,irole,ichoice};...
                                               pupil_trial_curve.Subject(isub).Role(irole).DecOption{(tdata.Price(t)),(tdata.Lottery(t))/2}.Pupilz(:)'];
            end            
            dec_option_mean{isub,irole,ichoice}=mean(dec_option{isub,irole,ichoice});
            dec_option_se{isub,irole,ichoice}=std(dec_option{isub,irole,ichoice})/sqrt(length(dec_option{isub,irole,ichoice}(:,1))-1);        
        
            %plot
            [l,p] = boundedline(x, dec_option_mean{isub,irole,ichoice}, dec_option_se{isub,irole,ichoice},'k','alpha');
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
        end
        
        set(ax,'xlim', [-0.5 1.5]);
        xticks(-0.5:0.5:1.5);
        set(ax,'TickDir','out');
        xlabel('Time relative to decision (seconds)');
        ylabel('Pupil size (adjusted)');
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on        
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\dec\curve\s',num2str(isub),'_pupil_dec_option.tif'], '-r200');
    hold off;   
    close;
end

%% Decision pupil -500 ms to 1500 ms locked to decision: group

x=-0.75:0.002:1.5;

sdata=sub_data;

    figure('Renderer', 'painters', 'Position', [10 10 850 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            dec_option_group{irole,ichoice}=[];
            for isub=1:nsub
                dec_option_group{irole,ichoice}=[dec_option_group{irole,ichoice};dec_option{isub,irole,ichoice}];
            end            
            dec_option_group_mean{irole,ichoice}=mean(dec_option_group{irole,ichoice});
            dec_option_group_se{irole,ichoice}=std(dec_option_group{irole,ichoice})/sqrt(length(dec_option_group{irole,ichoice}(:,1))-1);
            %plot
            [l,p] = boundedline(x, dec_option_group_mean{irole,ichoice}, dec_option_group_se{irole,ichoice},'k','alpha');
            l.LineWidth=1.5;
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
        end
        
        set(ax,'xlim', [-0.5 1.5]);
        xticks(-0.5:0.5:1.5);
        set(ax,'TickDir','out');
        xlabel('Time relative to decision (seconds)');
        ylabel('Pupil size (adjusted)');
        ylim([-0.7 0.1]);
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on        
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\dec\curve\group_pupil_dec_option_across_trials.tif'], '-r200');
    hold off;   
    close;
 
%% Decision pupil -500 ms to 1500 ms locked to decision: group_across_subjects

x=-0.75:0.002:1.5;

sdata=sub_data;

    figure('Renderer', 'painters', 'Position', [10 10 900 350]);
    for irole=1:nrole
        ax=subplot(1,2,irole);        
        for ichoice=1:nchoice
            dec_option_group{irole,ichoice}=[];
            for isub=1:nsub
                dec_option_group{irole,ichoice}=[dec_option_group{irole,ichoice};dec_option_mean{isub,irole,ichoice}];
            end            
            dec_option_group_mean{irole,ichoice}=mean(dec_option_group{irole,ichoice});
            dec_option_group_se{irole,ichoice}=std(dec_option_group{irole,ichoice})/sqrt(length(dec_option_group{irole,ichoice}(:,1))-1);
            %plot
            [l,p] = boundedline(x, dec_option_group_mean{irole,ichoice}, dec_option_group_se{irole,ichoice},'k','alpha');
            set(l,'Color',choice_color_dark{irole,ichoice});
            set(p,'FaceColor',choice_color_dark{irole,ichoice});  
        end
        
        set(ax,'xlim', [-0.5 1.5]);
        xticks(-0.5:0.5:1.5);
        set(ax,'TickDir','out');
        xlabel('Time relative to decision (seconds)');
        ylabel('Pupil size (adjusted)');
        line([0 0],ylim,'Color',[0 0 0],'LineStyle','-');
        title(role_label{irole});
        box on        
        hold on;
    end
    print(1, '-dtiff', [root_path,'\figure\pupil\dec\curve\group_pupil_dec_option_across_subjects.tif'], '-r200');
    hold off;   
    close;

%% 
toc;
